﻿using System;
using System.Linq;
using System.Web.UI;
using Azad;
using NazSoft.Framework;
using NazSoft.Framework.Extensions;
public partial class _AjKaAkhbar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var collection = Request.QueryString;
        String mn = collection["mn"] ?? "taza-tareen"; // menu name
        string mp = collection["mp"];
        bool preview = Convert.ToBoolean(collection["preview"]);

        if (Page.IsPostBack == false)
        {
            SilverlightService silverlightService = new SilverlightService();
            var today = DatetimeUtility.GetDateFromQueryString(collection);

            var logoInfo = silverlightService.GetByCurrentId(today, preview);
            var logoHeadlines = silverlightService.GetHeadlines();
            if (logoInfo != null)
            {

                // over-ride date here to be used later in databound method
                today = logoInfo.CurrentDate; ;


                var newsMarque = logoHeadlines.Detail.Where(x => x.MenuId == 12).FirstOrDefault();
                if (newsMarque != null)
                {
                    NewsRepeater1.DataSource = newsMarque.DetailToDetail;
                    NewsRepeater1.DataBind();
                }

                var details = logoInfo.Detail.Where(x => x.MenuId == 11).FirstOrDefault();

                if (details != null)
                {
                    // add tags to page meta tags
                    //this.Page.AddMetaTags("keywords", details.Tags);

                    // get detail to detail
                    var detailToDetail = details.DetailToDetail;
                    var pageSize = 1;//details.NoOfFilePerPage;
                    pageSize = pageSize == 0 ? 4 : pageSize;

                    var currentPage = (collection["page"].ToInt32Safe());
                    currentPage = currentPage == 0 ? 1 : currentPage;

                    var recordsToSkip = (currentPage - 1) * pageSize;
                    var totalPages = Math.Ceiling((double)(detailToDetail.Count / (double)pageSize));

                    var dataToDisplay = detailToDetail.Skip(recordsToSkip).Take(pageSize);


                    NewsRepeater.DataSource = dataToDisplay;
                    NewsRepeater.DataBind();

                    if (currentPage == 1)
                    {
                        if (detailToDetail.Count > pageSize)
                        {
                            NextPageHyperlink.Visible = true;
                            PrevPageHyperLink.Visible = false;
                        }
                        else
                        {
                            NextPageHyperlink.Visible = false;
                            PrevPageHyperLink.Visible = false;
                        }
                    }
                    else if (currentPage == totalPages)
                    {
                        NextPageHyperlink.Visible = false;
                        PrevPageHyperLink.Visible = true;
                    }
                    else
                    {
                        NextPageHyperlink.Visible = true;
                        PrevPageHyperLink.Visible = true;
                    }

                    if (today.HasValue)
                    {
                        var navigateUrl = string.Format("/{0}/{1}/{2}/{3}/", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad(), details.MenuName);


                        NextPageHyperlink.NavigateUrl = navigateUrl + "" + (currentPage + 1) + "";
                        PrevPageHyperLink.NavigateUrl = navigateUrl + "" + (currentPage - 1) + "";


                        if (mp == "b")
                        {
                            NextPageHyperlink.NavigateUrl = NextPageHyperlink.NavigateUrl + "?mp=b";
                            PrevPageHyperLink.NavigateUrl = PrevPageHyperLink.NavigateUrl + "?mp=b";
                        }



                        this.Page.Title = "NewsWeek | " + today.Value.ToString("D") + " | " + details.MenuName.Replace("-", " ").Capitalize();
                    }

                    AajKaAkhbarSharingLinksPlaceHolder.Visible = true;
                    //HomePageSharingLinksPlaceHolder.Visible = false;

                }
                else
                {
                    NextPageHyperlink.Visible = false;
                    PrevPageHyperLink.Visible = false;

                    this.Page.Title = "NewsWeek | " + today.Value.ToString("D") + " | " + mn.Replace("-", " ").Capitalize();


                    // set default meta tags
                    //this.Page.AddMetaTags("keywords", string.Empty);
                }


                //SetMagnify();


            }
            else
            {
                NewsRepeater.Visible = false;
                NoNewsMessage.Visible = true;
                NextPageHyperlink.Visible = false;
                PrevPageHyperLink.Visible = false;


                // set default meta tags
                //this.Page.AddMetaTags("keywords", string.Empty);
            }
        }
    }

    //    private void SetMagnify()
    //    {

    //        if (Request.QueryString["mag"] == "true")
    //        {
    //            string query = @"jQuery(document).ready(function ($) { 
    //                    $('.magnifiable').each(function(){
    //
    //                            $(this).addpowerzoom({defaultpower: 1.4,
    //                        
    //
    //                            powerrange: [80, 80],largeimage: true,magnifiersize: [500, 200] }); 
    //                        });
    //
    //});";
    //            //query = string.Format(query, imgFarman.ClientID);
    //            ScriptManager.RegisterStartupScript(this, this.GetType(), "Farman", query, true);

    //        }
    //        else
    //        {

    //            if (Request.QueryString["mag"] == "false")
    //            {
    //                Session["Magni"] = null;
    //            }





    //            if (!string.IsNullOrEmpty(Convert.ToString(Session["Magni"])))
    //            {
    //                string query = @"jQuery(document).ready(function ($) { 
    //                    $('.magnifiable').each(function(){
    //
    //                            $(this).addpowerzoom({defaultpower: 1.4,
    //                        
    //
    //                            powerrange: [80, 80],largeimage: true,magnifiersize: [500, 200] }); 
    //                        });
    //
    //});";
    //                //query = string.Format(query, imgFarman.ClientID);
    //                ScriptManager.RegisterStartupScript(this, this.GetType(), "LogoDetailDetail", query, true);


    //            }
    //        }
    //    }
}